package com.opdar.athena.web.controllers;

import com.opdar.athena.base.Constants;
import com.opdar.athena.base.IServerInfo;
import com.opdar.athena.base.RedisSessionManager;
import com.opdar.athena.entities.ProjectEntity;
import com.opdar.athena.entities.UserEntity;
import com.opdar.athena.registry.ZookeeperRegistry;
import com.opdar.athena.web.base.AthenaServerInfo;
import com.opdar.athena.web.interceptors.AuthViewInterceptor;
import com.opdar.athena.web.service.FunctionService;
import com.opdar.athena.web.service.ProjectService;
import com.opdar.athena.web.service.UserService;
import com.opdar.platform.annotations.Interceptor;
import com.opdar.platform.annotations.Request;
import com.opdar.platform.core.base.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by shiju on 2017/1/24.
 */
@Controller
@Interceptor(AuthViewInterceptor.class)
public class DashboardController {

    @Autowired
    FunctionService functionService;
    @Autowired
    UserService userService;
    @Autowired
    ProjectService projectService;
    @Autowired
    IServerInfo serverInfo;
    @Autowired
    private RedisSessionManager sessionManager;

    @Request(value = "/dashboard/index.html", format = Request.Format.VIEW)
    public Object login(String userName, String userPwd) {
        Context.putAttribute("sidebar", functionService.findSidebar());
        return "dashboard/index";
    }

    @Request(value = "/dashboard/sys.html", format = Request.Format.VIEW)
    public Object sys() {
        Context.putAttribute("sidebar", functionService.findSidebar());
        List<InetSocketAddress> addresses = serverInfo.getAddress();
        List<Map<String,Object >> addressList = new LinkedList<Map<String, Object>>();
        for(InetSocketAddress address:addresses){
            Map<String,Object> addressMap = new HashMap<String,Object>();
            String add = address.getAddress().getHostAddress() + ":" + address.getPort();
            long count = sessionManager.getClientCount(add);
            addressMap.put("address",add);
            addressMap.put("count",count);
            addressList.add(addressMap);
        }
        Context.putAttribute("addresses", addressList);

        return "dashboard/sys";
    }
    /**
     * 个人信息
     *
     * @return
     */
    @Request(value = "/dashboard/base/info.html", format = Request.Format.VIEW)
    public Object info(UserEntity userEntity) {
        Context.putAttribute("sidebar", functionService.findSidebar());
        if(userEntity!= null && userEntity.getId() != null){
            String token = (String) Context.getRequest().getSession().getAttribute("token");
            UserEntity user = sessionManager.get(token);
            UserEntity update = new UserEntity();
            UserEntity where = new UserEntity();
            where.setId(userEntity.getId());
            if(userEntity.getNickName() != null){
                update.setNickName(userEntity.getNickName());
                user.setNickName(userEntity.getNickName());
            }
            if(userEntity.getRealName() != null){
                update.setRealName(userEntity.getRealName());
                user.setRealName(userEntity.getRealName());
            }
            if(userEntity.getEmail() != null){
                update.setEmail(userEntity.getEmail());
                user.setEmail(userEntity.getEmail());
            }
            if(userEntity.getCellphone() != null){
                update.setCellphone(userEntity.getCellphone());
                user.setCellphone(userEntity.getCellphone());
            }
            if(userService.update(update,where)){
                sessionManager.set(token,user, Constants.SESSION_TIMEOUT);
                Context.putAttribute("user",user);
            }
        }
        return "dashboard/base/info";
    }

    @Request(value = "/dashboard/base/user.html", format = Request.Format.VIEW)
    public Object user() {
        Context.putAttribute("sidebar", functionService.findSidebar());
        Context.putAttribute("users", userService.selectSystemUserList());
        return "dashboard/base/user/index";
    }

    @Request(value = "/dashboard/channel/project.html", format = Request.Format.VIEW)
    public Object project() {
        List<ProjectEntity> projects = projectService.selectAll();
        Context.putAttribute("projects", projects);
        Context.putAttribute("sidebar", functionService.findSidebar());
        return "dashboard/channel/project/index";
    }
}
